<?php
/*
This sends a little bit of JSON to say if the user is logged in. 
*/

require_once( 'core.php' );
require_once( 'current_user_api.php' );
require_once( 'bug_api.php' );


auth_ensure_user_authenticated( );
//helper_begin_long_process( );

$t_plugin_path = config_get( 'plugin_path' );
require_once( $t_plugin_path . 'DataRefine' . DIRECTORY_SEPARATOR . 'TableMetadata.php' );
require_once( $t_plugin_path . 'DataRefine' . DIRECTORY_SEPARATOR . 'JSONstore.php' );
require_once( $t_plugin_path . 'DataRefine' . DIRECTORY_SEPARATOR . 'MetadataStandards.php' );
require_once( $t_plugin_path . 'DataRefine' . DIRECTORY_SEPARATOR . 'ProjectsMetadata.php' );
require_once( $t_plugin_path . 'DataRefine' . DIRECTORY_SEPARATOR . 'HTTPclient.php' );
require_once( $t_plugin_path . 'DataRefine' . DIRECTORY_SEPARATOR . 'TableOutputs.php' );

/*
 Turn this on to update the metadata standards
 
$MetadataStandards = new MetadataStandards;
$MetadataStandards->saveMetadata();
$MetadataStandards->datasetMetadata();
*/

$MetadataStandards = new MetadataStandards;
$MetadataStandards->datasetMetadata();


$authHashPrefix = plugin_config_get( 'authHashPrefix' );
$refineHumanURI = plugin_config_get( 'refineBaseURI' ); //for human linking
$refineURI = plugin_config_get( 'refineLocalURI' );
if(!$refineURI){
	$refineURI = plugin_config_get( 'refineBaseURI' );
}

# extracts the user information for the currently logged in user
# and prefixes it with u_
$f_user_id = gpc_get_int( 'id', auth_get_current_user_id() );
$row = user_get_row( $f_user_id );

extract( $row, EXTR_PREFIX_ALL, 'u' );
$u_email = user_get_email( $u_id );
$u_realname = user_get_realname( $u_id );

if(!isset($_GET["grProject"])){
	$GRprojectID =  false;
}
else{
	$GRprojectID = $_GET["grProject"]; 
}



$tableMetaObj = new TableMetadata;
$tableMetaObj->refineURI = $refineURI;
$tableMetaObj->refineHumanURI = $refineHumanURI;
$tableMetaObj->authHashPrefix = $authHashPrefix;
$tableMetaObj->currentUserID = $f_user_id;
$tableMetaObj->currentUserName = $u_username;
$tableMetaObj->GRprojectID = $GRprojectID;
$tableMetaObj->get_metadata();
$datasetMetadata = $tableMetaObj->get_descriptive_fields();

$tableMetaObj->count_description_done(); //count number of completed general descriptive metadata fields;
$tableMetaObj = check_edit_resolution($tableMetaObj); //now check on the status of the edit issue
$tableMetaObj = check_metadata_fieldDes_resolution($tableMetaObj); //now check on status of the metadata desription and field documentation issues
$tableMetadata = $tableMetaObj->currentMetadata;

$tableMetaObj->count_description_done();
$tableMetaObj->count_column_documentation();
$tableMetadata = $tableMetaObj->currentMetadata;

if(!check_dataset_projectID_auth($tableMetadata)){
	break; //not authorized to view, stop showing anything else!
}

html_page_top( "Data Documentation: Dataset Issue Tracking Summary" );


?>

<div class="data-doc-page-body">
	
	<!-- page heading information -->
	<div class="data-doc-page-heading">
		<div class="data-doc-page-heading-row">
			<div class="data-doc-page-heading-title">
				<h2>General Descriptive Metadata</h2>
				<p>Issues and Summary Status: [<?php echo "<a href=\"../plugin.php?page=DataRefine/data-doc-summary&grProject=".$GRprojectID."\">Link</a>"; ?>]</p>
			</div>
			<div class="data-doc-page-heading-dset">
				<div class="data-doc-page-heading-dset-tab">
					<div class="data-doc-page-heading-dset-row">
						<div class="data-doc-page-heading-dset-title">
							<?php
							if(isset($tableMetadata["description"][0]["values"][0]["text"])){
								echo "<h3>&ldquo;".$tableMetadata["description"][0]["values"][0]["text"]."&rdquo;</h3>";
								echo "<br/><br/><span class=\"dest-sub-title\">(In refine as: <em>".$tableMetadata["name"]."</em>)</span>";	
							}
							else{
								echo "<h3>&ldquo;".$tableMetadata["name"]."&rdquo;</h3>";
							}
							
							?>
						</div>
						<div class="data-doc-page-heading-dset-des">
						This table currently has <span id="dataset-row-count"><?php echo $tableMetadata["refineRowCount"];  ?></span> rows. The last edit / modification of this table in Google Refine took place on <span id="dataset-last-modified"><?php echo date("F j, Y, g:i a", strtotime($tableMetadata["refineModified"]));  ?></span>. View the dataset in Google refine: [<a href="<?php echo $refineHumanURI."/project?project=".$GRprojectID;  ?>">Here</a>]
						<br/><br/>
						Number of required general descriptive metadata fields complete: <span id="show_reqFieldsCompleted"><?php echo $tableMetaObj->descriptionsDone; ?></span> of <span id="show_reqFieldsTotal"><?php echo $tableMetaObj->numberRequiredDescript; ?></span>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>

	<!-- Main content for this dataset tracking page -->	
	<div class="data-doc-page-primary">
	
		
			
			<input type="hidden" id="GRprojectID" value="<?php echo $GRprojectID; ?>" />
			<input type="hidden" id="reqFieldsCompleted" value="<?php echo $tableMetaObj->descriptionsDone; ?>" />
			<input type="hidden" id="reqFieldsTotal" value="<?php echo $tableMetaObj->numberRequiredDescript; ?>" />
			<input type="hidden" id="userID" value="<?php echo $u_username; ?>" />
			
			<table class="fieldTable" >
				<!--
				<thead>
					<th>Field</th>
					<th>Value</th>
				</thead>
				<tbody>
				-->
			<?php
			
			if(is_array($datasetMetadata)){
				
				$i = 0;
				foreach($datasetMetadata["fields"] as $fieldKey => $fieldArray){
					if ( $i&1 ){
						$style = "style='background-color:#ECF1EF;'";
					}
					else{
						$style = "";
					}
					
					echo "<tr ".$style.">\n";
				
					echo "<td class=\"field_name_cell\">";
					echo "<span class=\"field_name\">".ucwords($fieldArray["name"])."</span>";
					echo "<br/>";
					echo "<span class=\"field_note\">".$fieldArray["note"]."</span>";
					if($fieldArray["required"]){
						echo "<br/>";
						echo "<span class=\"field_required\">Required</span>";
					}
					echo "</td>\n";
					
					echo "<td class=\"field_val_cell\">";
					
					echo "<div id=\"field_tab_".$fieldKey."\" class=\"div_val_tab\">".chr(13);
					
					
					$countSetValues = 0;
					if(isset($tableMetadata["description"][$fieldKey]["values"])){
						if(is_array($tableMetadata["description"][$fieldKey]["values"])){
							foreach($tableMetadata["description"][$fieldKey]["values"] as $valIndex => $valArray){
								if(isset($valArray["text"])){
									$countSetValues++;
								}
							}
						}
						else{
							$tableMetadata["description"][$fieldKey]["values"][0] = false;
						}
					}
					else{
						$tableMetadata["description"][$fieldKey]["values"][0] = false;
					}
					
					//add values		
					foreach($tableMetadata["description"][$fieldKey]["values"] as $valIndex => $valArray){
						
						if(($countSetValues == 0 && $valIndex == 0) || isset($valArray["text"])){
							echo "<div id=\"field_DivRow_".$fieldKey."_".$valIndex."\" class=\"div_val_row\">".chr(13);
							echo "<div class=\"div_val_cell\">".chr(13);
								
							$valInput = "<textarea id=\"field_val_".$fieldKey."_".$valIndex."\" class=\"".$fieldArray["type"]."\" onChange=\"addFieldValue(this.id, '".$fieldKey."', ".$valIndex.")\">";
							
							if(isset($valArray["text"])){
								$valInput .= $valArray["text"];
							}
							
							$valInput .= "</textarea>".chr(13);
							echo $valInput;
							echo "<br/>";
							
							echo "<div id=\"field_noteCreator_".$fieldKey."_".$valIndex."\" class=\"noteCreator\">".chr(13);
							if(isset($valArray["noteCreator"])){
								echo"Documented by: ".$valArray["noteCreator"];
							}
							
							echo "</div>";
							echo "</div>".chr(13);
							
							echo "<div class=\"div_val_cont_cell\" id=\"field_val_com_".$fieldKey."_".$valIndex."\">".chr(13);
							
							$needRemControl = false;
							if($valArray["text"] != false){
								$needRemControl = true;	
								echo "<a id=\"val_multiRemove_".$fieldKey."_".$valIndex."\" href=\"javascript:remMultiValue(this.id, '".$fieldKey."', ".$valIndex.")\" class=\"rem_multivalue\">Remove documentation</a>"; 
								
							}
							
							echo "</div>"; // end div cell for controls
							echo "</div>"; //end row
						}
					}
					
					echo "</div>".chr(13); //end div table
					
					if($fieldArray["multipleOK"]){				
						echo "<a id=\"addMulti_".$fieldKey."\" href=\"javascript:addMultiValue(this.id, '".$fieldArray["type"]."', '".$fieldKey."', ".$valIndex.")\" class=\"add_multivalue\">Add another</a>";		
					}
					
					echo "</td>\n";
					echo "</tr>\n";
				$i++;
				}//end loop through metadata
			}
			?>
				</tbody>
			</table>
		</div>
	</div>
</div>

	

<?php
html_page_bottom();
?>

